#include <iostream>
#include <ctime>
#include "queue.h"
#include "customer.h"

const int MIN_PER_HOUR = 60;

bool newcustomer(double x);

int main(int argc, char const *argv[])
{
    using std::cin;
    using std::cout;
    using std::endl;
    using std::ios_base;
    std::srand(std::time(0));

    cout << "Maximum size of queue:";
    int qs;
    cin >> qs;
    Queue line(qs);

    cout << endl << "模拟的小时数:";
    int hours;
    cin >> hours;
    long cyclelimit = MIN_PER_HOUR * hours; // 1 分钟模拟 1 次

    cout << endl << "每小时顾客数:";
    double perhour;
    cin >> perhour;
    double min_per_cust;
    min_per_cust = MIN_PER_HOUR / perhour;  // 平均多少分钟 1 个人

    // 新客
    Item temp;
    long turnaways = 0;
    long customers = 0;
    long served = 0;
    long sum_line = 0;
    int wait_time = 0;
    long line_wait = 0;

    for (int cycle = 0; cycle < cyclelimit; ++cycle)
    {
        if (newcustomer(min_per_cust)) // 有新顾客
        {
            if (line.isfull())
                turnaways++;
            else
            {
                customers++;
                temp.set(cycle); // 设置到达时间
                line.enqueue(temp);
            }
        }

        if (wait_time <= 0 && !line.isempty())
        {
            line.dequeue(temp);
            wait_time = temp.ptime();
            line_wait = cycle - temp.when();
            served++;
        }

        if (wait_time > 0)
            wait_time --;
        
        sum_line += line.queuecount();
    }



    

    return 0;
}

bool newcustomer(double x)
{
    return (std::rand() * x / RAND_MAX < 1);
}